﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmSupply : Form
    {
        private string opState;

        private IBLL.ISupply typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();

        public FrmSupply()
        {
            InitializeComponent();
        }

        private void btnSek_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.FlatAppearance.BorderSize = 0;
            Point point = new Point(btn.Left + this.Left + 5, btn.Top + this.Top + 40);
            contextMenuAssign.Show(point);
        }

        private void ByCodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] value = CusInputBox.Input("请输入查询条件：", "编号", "名称", "");
            if (value != null && value.Length >= 2)
            {
                if (!string.IsNullOrEmpty(value[0]))
                {
                    MisModel.Supply model = typeBLL.GetById(value[0].Trim());
                    if (model != null)
                    {
                        this.txtSuppID.Text = model.SuppID;
                        if (model.F1 == "Y")
                        {
                            this.chkF1.Checked = true;
                        }
                        else
                        {
                            this.chkF1.Checked = false;
                        }
                        this.txtF2.Text = model.F2;
                        this.txtFaithje.Text = model.Faithje.ToString();
                        this.txtDebt.Text = model.Debt.ToString();
                        this.txtSupply.Text = model.SuppName;
                        this.txtBoss.Text = model.Boss;
                        this.txtAddr.Text = model.Addr;
                        this.txtPost.Text = model.Post;
                        this.txtTele.Text = model.Tele;
                        this.txtAllName.Text = model.AllName;
                        this.txtBank.Text = model.Bank;
                        this.txtBankNo.Text = model.BankNo;
                        this.txtTaxNo.Text = model.TaxNo;
                        this.txtLinker.Text = model.Liker;
                        this.txtBei.Text = model.Bei;
                        this.dtpDateF.Text = model.DateF.ToString();
                        this.txtSumjeBuy.Text = model.SumJeBuy.ToString();
                        this.dtpDateL.Text = model.DateL.ToString();
                    }
                }
                else if (!string.IsNullOrEmpty(value[1]))
                {
                    DataTable dt = typeBLL.GetByName(value[1]);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        this.lbxList.DisplayMember = "ItemShow";
                        this.lbxList.DataSource = dt;
                        this.lbxList.Show();
                    }
                    else
                    {
                        MessageBox.Show(string.Format("没有查询到（{0}）", value[1]), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

        }

        private void ChoiseListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.lbxList.Show();
        }

        private void lbxList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DataRowView view = this.lbxList.SelectedItem as DataRowView;
            this.txtSuppID.Text = view.Row["SUPP_ID"].ToString();
            if (view.Row["F1"].ToString() == "Y")
            {
                this.chkF1.Checked = true;
            }
            else
            {
                this.chkF1.Checked = false;
            }
            this.txtF2.Text = view.Row["F2"].ToString();
            this.txtFaithje.Text = view.Row["FAITHJE"].ToString();
            this.txtDebt.Text = view.Row["DEBT"].ToString();
            this.txtSupply.Text = view.Row["SUPPLY"].ToString();
            this.txtBoss.Text = view.Row["BOSS"].ToString();
            this.txtAddr.Text = view.Row["ADDR"].ToString();
            this.txtPost.Text = view.Row["POST"].ToString();
            this.txtTele.Text = view.Row["TELE"].ToString();
            this.txtAllName.Text = view.Row["ALLNAME"].ToString();
            this.txtBank.Text = view.Row["BANK"].ToString();
            this.txtBankNo.Text = view.Row["BANKNO"].ToString();
            this.txtTaxNo.Text = view.Row["TAXNO"].ToString();
            this.txtLinker.Text = view.Row["LINKER"].ToString();
            this.txtBei.Text = view.Row["BEI"].ToString();
            this.dtpDateF.Text = view.Row["DATE_F"].ToString();
            this.txtSumjeBuy.Text = view.Row["SUMJEBUY"].ToString();
            this.dtpDateL.Text = view.Row["DATE_L"].ToString();
            this.lbxList.Hide();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtSuppID.ReadOnly = false;
            this.chkF1.Enabled = true;
            this.txtF2.ReadOnly = false;
            this.txtFaithje.ReadOnly = false;
            this.txtDebt.ReadOnly = false;
            this.txtSupply.ReadOnly = false;
            this.txtBoss.ReadOnly = false;
            this.txtAddr.ReadOnly = false;
            this.txtPost.ReadOnly = false;
            this.txtTele.ReadOnly = false;
            this.txtAllName.ReadOnly = false;
            this.txtBank.ReadOnly = false;
            this.txtBankNo.ReadOnly = false;
            this.txtTaxNo.ReadOnly = false;
            this.txtLinker.ReadOnly = false;
            this.txtBei.ReadOnly = false;
            this.dtpDateF.Enabled = true;
            this.dtpDateL.Enabled = true;

            this.btnAdd.Enabled = false;
            this.btnDel.Enabled = false;
            this.btnMod.Enabled = false;
            this.btnSek.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnRef.Enabled = false;
            this.btnQuit.Enabled = false;
            this.btnOk.Enabled = true;
            this.btnCan.Enabled = true;

            this.txtSuppID.Text = "";
            this.chkF1.Checked = true;
            this.txtF2.Text = "U";
            this.txtFaithje.Text = "";
            this.txtDebt.Text = "";
            this.txtSupply.Text = "";
            this.txtBoss.Text = "";
            this.txtAddr.Text = "";
            this.txtPost.Text = "";
            this.txtTele.Text = "";
            this.txtAllName.Text = "";
            this.txtBank.Text = "";
            this.txtBankNo.Text = "";
            this.txtTaxNo.Text = "";
            this.txtLinker.Text = "";
            this.txtBei.Text = "";
            this.dtpDateF.Text = DateTime.Now.ToString();
            this.dtpDateL.Text = DateTime.Now.ToString();
            this.txtSuppID.Focus();
            this.opState = "添加";
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            this.txtSuppID.ReadOnly = true;
            this.chkF1.Enabled = false;
            this.txtF2.ReadOnly = true;
            this.txtFaithje.ReadOnly = true;
            this.txtDebt.ReadOnly = true;
            this.txtSupply.ReadOnly = true;
            this.txtBoss.ReadOnly = true;
            this.txtAddr.ReadOnly = true;
            this.txtPost.ReadOnly = true;
            this.txtTele.ReadOnly = true;
            this.txtAllName.ReadOnly = true;
            this.txtBank.ReadOnly = true;
            this.txtBankNo.ReadOnly = true;
            this.txtTaxNo.ReadOnly = true;
            this.txtLinker.ReadOnly = true;
            this.txtBei.ReadOnly = true;
            this.dtpDateF.Enabled = false;
            this.dtpDateL.Enabled = false;

            this.btnAdd.Enabled = true;
            this.btnDel.Enabled = true;
            this.btnMod.Enabled = true;
            this.btnSek.Enabled = true;
            this.btnPrint.Enabled = true;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = true;
            this.btnOk.Enabled = false;
            this.btnCan.Enabled = false;
            this.btnAdd.Focus();

            DataRowView view = this.lbxList.SelectedItem as DataRowView;
            if (view != null)
            {
                this.txtSuppID.Text = view.Row["SUPP_ID"].ToString();
                if (view.Row["F1"].ToString() == "Y")
                {
                    this.chkF1.Checked = true;
                }
                else
                {
                    this.chkF1.Checked = false;
                }
                this.txtF2.Text = view.Row["F2"].ToString();
                this.txtFaithje.Text = view.Row["FAITHJE"].ToString();
                this.txtDebt.Text = view.Row["DEBT"].ToString();
                this.txtSupply.Text = view.Row["SUPPLY"].ToString();
                this.txtBoss.Text = view.Row["BOSS"].ToString();
                this.txtAddr.Text = view.Row["ADDR"].ToString();
                this.txtPost.Text = view.Row["POST"].ToString();
                this.txtTele.Text = view.Row["TELE"].ToString();
                this.txtAllName.Text = view.Row["ALLNAME"].ToString();
                this.txtBank.Text = view.Row["BANK"].ToString();
                this.txtBankNo.Text = view.Row["BANKNO"].ToString();
                this.txtTaxNo.Text = view.Row["TAXNO"].ToString();
                this.txtLinker.Text = view.Row["LINKER"].ToString();
                this.txtBei.Text = view.Row["BEI"].ToString();
                this.dtpDateF.Text = view.Row["DATE_F"].ToString();
                this.txtSumjeBuy.Text = view.Row["SUMJEBUY"].ToString();
                this.dtpDateL.Text = view.Row["DATE_L"].ToString();
            }
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            this.txtSuppID.ReadOnly = true;
            this.chkF1.Enabled = true;
            this.txtF2.ReadOnly = false;
            this.txtFaithje.ReadOnly = false;
            this.txtDebt.ReadOnly = false;
            this.txtSupply.ReadOnly = false;
            this.txtBoss.ReadOnly = false;
            this.txtAddr.ReadOnly = false;
            this.txtPost.ReadOnly = false;
            this.txtTele.ReadOnly = false;
            this.txtAllName.ReadOnly = false;
            this.txtBank.ReadOnly = false;
            this.txtBankNo.ReadOnly = false;
            this.txtTaxNo.ReadOnly = false;
            this.txtLinker.ReadOnly = false;
            this.txtBei.ReadOnly = false;
            this.dtpDateF.Enabled = true;
            this.dtpDateL.Enabled = true;

            this.btnAdd.Enabled = false;
            this.btnDel.Enabled = false;
            this.btnMod.Enabled = false;
            this.btnSek.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnRef.Enabled = false;
            this.btnQuit.Enabled = false;
            this.btnOk.Enabled = true;
            this.btnCan.Enabled = true;
            this.txtSupply.Focus();
            this.opState = "修改";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.opState))
            {
                bool isOk = false;
                if (this.opState == "添加")
                {
                    isOk = Add();
                }
                else if (this.opState == "修改")
                {
                    isOk = Mod();
                }

                if (isOk)
                {
                    this.txtSuppID.ReadOnly = true;
                    this.chkF1.Enabled = false;
                    this.txtF2.ReadOnly = true;
                    this.txtFaithje.ReadOnly = true;
                    this.txtDebt.ReadOnly = true;
                    this.txtSupply.ReadOnly = true;
                    this.txtBoss.ReadOnly = true;
                    this.txtAddr.ReadOnly = true;
                    this.txtPost.ReadOnly = true;
                    this.txtTele.ReadOnly = true;
                    this.txtAllName.ReadOnly = true;
                    this.txtBank.ReadOnly = true;
                    this.txtBankNo.ReadOnly = true;
                    this.txtTaxNo.ReadOnly = true;
                    this.txtLinker.ReadOnly = true;
                    this.txtBei.ReadOnly = true;
                    this.dtpDateF.Enabled = false;
                    this.dtpDateL.Enabled = false;

                    this.btnAdd.Enabled = true;
                    this.btnDel.Enabled = true;
                    this.btnMod.Enabled = true;
                    this.btnSek.Enabled = true;
                    this.btnPrint.Enabled = true;
                    this.btnRef.Enabled = true;
                    this.btnQuit.Enabled = true;
                    this.btnOk.Enabled = false;
                    this.btnCan.Enabled = false;
                    this.btnAdd.Focus();
                }
            }
        }

        //添加供应商
        protected bool Add()
        {
            if (this.txtSuppID.Text.Trim().Length == 0 || this.txtSuppID.Text.Trim().Length != 9)
            {
                MessageBox.Show("编码长度不符，应为9！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtSuppID.Focus();
                return false;
            }
            if (this.txtSupply.Text.Trim().Length == 0 || this.txtSupply.Text.Trim().Length > 20)
            {
                MessageBox.Show("字符长度应介于1－20！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtSupply.Focus();
                return false;
            }
            if (this.txtAllName.Text.Trim().Length == 0 || this.txtAllName.Text.Trim().Length > 40)
            {
                MessageBox.Show("字符长度应介于1－40！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtAllName.Focus();
                return false;
            }


            MisModel.Supply obj = new MisModel.Supply();

            obj.SuppID = this.txtSuppID.Text;
            if (this.chkF1.Checked)
            {
                obj.F1 = "Y";
            }
            else
            {
                obj.F1 = "N";
            }
            obj.F2 = this.txtF2.Text;
            if (!string.IsNullOrEmpty(this.txtFaithje.Text))
                obj.Faithje = decimal.Parse(this.txtFaithje.Text);
            else
                obj.Faithje = 0;
            if (!string.IsNullOrEmpty(this.txtDebt.Text))
                obj.Debt = decimal.Parse(this.txtDebt.Text);
            obj.SuppName = this.txtSupply.Text;
            obj.Boss = this.txtBoss.Text;
            obj.Addr = this.txtAddr.Text;
            obj.Post = this.txtPost.Text;
            obj.Tele = this.txtTele.Text;
            obj.AllName = this.txtAllName.Text;
            obj.Bank = this.txtBank.Text;
            obj.BankNo = this.txtBankNo.Text;
            obj.TaxNo = this.txtTaxNo.Text;
            obj.Liker = this.txtLinker.Text;
            obj.Bei = this.txtBei.Text;
            obj.DateF = DateTime.Parse(this.dtpDateF.Text);
            obj.DateL = DateTime.Parse(this.dtpDateL.Text);

            int ok = typeBLL.Add(obj);
            if (ok == -1)
            {
                MessageBox.Show("操作失败！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            else
            {
                MessageBox.Show("本条记录已成功添加！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return true;
        }

        //保存供应商
        protected bool Mod()
        {
            if (this.txtSupply.Text.Trim().Length == 0 || this.txtSupply.Text.Trim().Length > 20)
            {
                MessageBox.Show("字符长度应介于1－20！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtSupply.Focus();
                return false;
            }
            if (this.txtAllName.Text.Trim().Length == 0 || this.txtAllName.Text.Trim().Length > 40)
            {
                MessageBox.Show("字符长度应介于1－40！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtAllName.Focus();
                return false;
            }

            MisModel.Supply obj = new MisModel.Supply();
            obj.SuppID = this.txtSuppID.Text;
            if (this.chkF1.Checked)
            {
                obj.F1 = "Y";
            }
            else
            {
                obj.F1 = "N";
            }
            obj.F2 = this.txtF2.Text;
            obj.Faithje = decimal.Parse(this.txtFaithje.Text);
            obj.Debt = decimal.Parse(this.txtDebt.Text);
            obj.SuppName = this.txtSupply.Text;
            obj.Boss = this.txtBoss.Text;
            obj.Addr = this.txtAddr.Text;
            obj.Post = this.txtPost.Text;
            obj.Tele = this.txtTele.Text;
            obj.AllName = this.txtAllName.Text;
            obj.Bank = this.txtBank.Text;
            obj.BankNo = this.txtBankNo.Text;
            obj.TaxNo = this.txtTaxNo.Text;
            obj.Liker = this.txtLinker.Text;
            obj.Bei = this.txtBei.Text;
            obj.DateF = DateTime.Parse(this.dtpDateF.Text);
            obj.DateL = DateTime.Parse(this.dtpDateL.Text);

            int ok = typeBLL.Update(obj);
            if (ok == -1)
            {
                MessageBox.Show("操作失败！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            else
            {
                MessageBox.Show("本条记录已成功修改！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return true;
        }


        private void btnDel_Click(object sender, EventArgs e)
        {
            string statCode = this.txtSuppID.Text.Trim();
            if (string.IsNullOrEmpty(statCode))
                return;

            if (MessageBox.Show("请确认删除[y/n]？", "提问窗", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                int ok = typeBLL.Delete(statCode);
                if (ok == -1)
                {
                    MessageBox.Show("操作失败！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show("本条记录已成功删除！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.txtSuppID.Text = "";
                    this.chkF1.Checked = true;
                    this.txtF2.Text = "U";
                    this.txtFaithje.Text = "";
                    this.txtDebt.Text = "";
                    this.txtSupply.Text = "";
                    this.txtBoss.Text = "";
                    this.txtAddr.Text = "";
                    this.txtPost.Text = "";
                    this.txtTele.Text = "";
                    this.txtAllName.Text = "";
                    this.txtBank.Text = "";
                    this.txtBankNo.Text = "";
                    this.txtTaxNo.Text = "";
                    this.txtLinker.Text = "";
                    this.txtBei.Text = "";
                    this.dtpDateF.Text = DateTime.Now.ToString();
                    this.dtpDateL.Text = DateTime.Now.ToString();
                    this.btnDel.Enabled = false;
                    this.btnMod.Enabled = false;
                }
            }
        }

        private void FrmSupply_Load(object sender, EventArgs e)
        {
            this.txtSuppID.ReadOnly = true;
            this.chkF1.Enabled = false;
            this.txtF2.ReadOnly = true;
            this.txtFaithje.ReadOnly = true;
            this.txtDebt.ReadOnly = true;
            this.txtSupply.ReadOnly = true;
            this.txtBoss.ReadOnly = true;
            this.txtAddr.ReadOnly = true;
            this.txtPost.ReadOnly = true;
            this.txtTele.ReadOnly = true;
            this.txtAllName.ReadOnly = true;
            this.txtBank.ReadOnly = true;
            this.txtBankNo.ReadOnly = true;
            this.txtTaxNo.ReadOnly = true;
            this.txtLinker.ReadOnly = true;
            this.txtBei.ReadOnly = true;
            this.dtpDateF.Enabled = false;
            this.dtpDateL.Enabled = false;

            this.btnAdd.Enabled = true;
            this.btnDel.Enabled = true;
            this.btnMod.Enabled = true;
            this.btnSek.Enabled = true;
            this.btnPrint.Enabled = true;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = true;
            this.btnOk.Enabled = false;
            this.btnCan.Enabled = false;
            this.btnAdd.Focus();

            this.lbxList.DisplayMember = "ItemShow";
            this.lbxList.DataSource = typeBLL.GetAll();
        }


        private void txtF2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtFaithje.Focus();
        }

        private void txtFaithje_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtDebt.Focus();
        }

        private void txtDebt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtSupply.Focus();
        }

        private void txtSupply_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtBoss.Focus();
        }

        private void txtBoss_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtAddr.Focus();
        }

        private void txtAddr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtPost.Focus();
        }

        private void txtPost_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtTele.Focus();
        }

        private void txtTele_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtAllName.Focus();
        }

        private void txtAllName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtBank.Focus();
        }

        private void txtBank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtBankNo.Focus();
        }

        private void txtBankNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtTaxNo.Focus();
        }

        private void txtTaxNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtLinker.Focus();
        }

        private void txtLinker_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtBei.Focus();
        }

        private void txtBei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.dtpDateF.Focus();
        }

        private void dtpDateF_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtSumjeBuy.Focus();
        }

        private void txtSumjeBuy_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.dtpDateL.Focus();
        }

        private void dtpDateL_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //    this.btnOk_Click(sender, e);
        }

        private void lbxList_Leave(object sender, EventArgs e)
        {
            this.lbxList.Hide();
        }

        private void FrmSupply_Click(object sender, EventArgs e)
        {
            this.lbxList.Hide();
        }

        private void txtSuppID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.txtF2.Focus();
        }

        private void txtBei_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                this.txtF2.Focus();
        }
    }
}
